﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.Threading;

#endregion

namespace ScrumTable.Common.Threading
{
    /// <summary>
    /// Represents a critical section (Monitor) handler as disposable object.
    /// </summary>
    public class CriticalSection<T> : CriticalSection
        where T : class
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the synchronisation root node.
        /// </summary>
        protected T SyncRoot
        {
            get { return (T) SyncRootRaw; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CriticalSection instance. The specified argument
        /// declares the synchronization object.
        /// </summary>
        /// <param name="sync">Object which is used to synchronize the calls.</param>
        public CriticalSection(T sync)
            : base(sync)
        {
        }

        #endregion
    }


    /// <summary>
    /// Represents a critical section (Monitor) handler as disposable object.
    /// </summary>
    public class CriticalSection : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the synchronisation root node.
        /// </summary>
        protected object SyncRootRaw
        {
            get;
            private set;
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CriticalSection instance. The specified argument
        /// declares the synchronization object.
        /// </summary>
        /// <param name="sync">Object which is used to synchronize the calls.</param>
        public CriticalSection(object sync)
        {
            PreCondition.AssertNotNull(sync, "sync");

            SyncRootRaw = sync;
            Monitor.Enter(SyncRootRaw);
        }

        #endregion


        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Temporary releases the current lock. After leaving the section,
        /// you should call the Dispose method. This should be used in
        /// conjunction with the using() statement.
        /// </summary>
        /// <returns>Returns the dispose object to reenter the critical
        /// section.</returns>
        public IDisposable TemporaryReleaseLock()
        {
            return new TemporaryRelease(this);
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            Monitor.Exit(SyncRootRaw);
        }

        #endregion


        private class TemporaryRelease : DisposeBase
        {
            #region Declarations
            //--------------------------------------------------------------------
            // Declarations
            //--------------------------------------------------------------------

            private readonly CriticalSection _parent;

            #endregion

            #region Constructors / Destructor
            //--------------------------------------------------------------------
            // Constructors / Destructor
            //--------------------------------------------------------------------

            internal TemporaryRelease(CriticalSection parent)
            {
                PreCondition.AssertNotNull(parent, "parent");

                _parent = parent;
                Monitor.Exit(_parent.SyncRootRaw);
            }

            #endregion


            #region Methods
            //--------------------------------------------------------------------
            // Methods
            //--------------------------------------------------------------------

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                Monitor.Enter(_parent.SyncRootRaw);
            }

            #endregion
        }
    }
}